<?php

namespace app\admin\controller;

use app\admin\model\Fake as ModelFake;
use app\BaseController;
use frame\lib\Db;

class Fake extends Base
{
    public function index()
    {
        $this->view('index');
    }
    public function list()
    {
        $fake = @$_GET['fake'];
        success(ModelFake::list($fake));
    }
    public function edit()
    {
        $data = Db::name('fake')->where(['id' => $_GET['id']])->find();
        $this->view('edit', ['data' => $data]);
    }
    public function doEdit()
    {
        $param = $_POST;
        if (!empty($param['password'])) {
            $param['password'] = md5($param['password'] . config('secure', 'secure.salt'));
        } else unset($param['password']);
        $res = Db::name('fake')->where(['id' => $param['id']])->update($param);
        if ($res) success();
        error();
    }
    public function del()
    {
        $id = explode(',', $_GET['id']);
        $res = Db::name('fake')->whereIn('id', $id)->delete();
        if ($res) success();
        error();
    }
    public function add()
    {
        $this->view('add');
    }
    public function doAdd()
    {
        $param = $_POST;
        if ($param['num']) {
            if ($param['type'] == 2) {
                if (!array_key_exists('vip_count', $param) || !array_key_exists('vip_type', $param)) {
                    error('缺少参数vip_count、vip_type', 201);
                }
            } else if ($param['type'] == 3) {
                if (!array_key_exists('score', $param)) error('缺少参数score', 201);
                if(!is_numeric($param['score'])) error('score类型错误');
                if($param['score'] <= 0) error('积分值必须大于0');
            }
            
            ModelFake::add($param);
            success();
        }
        error('请将参数输入完整', 201);
    }
}
